import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

/*
* @Package: http
* @ClassName: httpManager
* @Author: szj
* @CreateDate: 7/28/21 4:05 PM
*/
class HttpManager<T> {
  static Utf8Decoder utf8decoder = Utf8Decoder();

  //私有化构造函数
  HttpManager._();

  //获取String数据
  static requestHttpData<T>(String url,
      {@required Function onSuccess,
      Function onFail,
      Function complete}) async {
    try {
      var response = await http.get(Uri.parse(url));
      print("szj网络数据code:${response.statusCode}}\n接口为:$url");
      //网络请求成功
      if (response.statusCode == 200) {
        dynamic result = json.decode(utf8decoder.convert(response.bodyBytes));

        print("szj网络数据成功返回:$result");
        onSuccess(result);
      }
    } catch (e) {
      print("szj网络数据失败返回:$e");
      onFail(e);
    } finally {
      if (complete != null) {
        complete();
      }
    }
  }
}
